﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using UnityEngine;

    public class VCOverlay
    {
        [CompilerGenerated]
        private static Dictionary<string, int> <>f__switch$map17;
        private const string kPerforceIconPrefix = "P4_";
        private const string kSubversionIconPrefix = "SVN_";
        private static VCAsset m_Asset;
        private static string m_IconPrefix;
        private static Rect m_ItemRect;
        private static Dictionary<string, Texture2D> overlays = new Dictionary<string, Texture2D>();

        public static void DrawOverlay(VCAsset asset, Rect itemRect)
        {
            if ((asset != null) && (Event.current.type == EventType.Repaint))
            {
                m_Asset = asset;
                m_ItemRect = itemRect;
                string externalVersionControl = EditorSettings.externalVersionControl;
                if (externalVersionControl != null)
                {
                    int num;
                    if (<>f__switch$map17 == null)
                    {
                        Dictionary<string, int> dictionary = new Dictionary<string, int>(2);
                        dictionary.Add("Subversion", 0);
                        dictionary.Add("Perforce", 1);
                        <>f__switch$map17 = dictionary;
                    }
                    if (<>f__switch$map17.TryGetValue(externalVersionControl, out num))
                    {
                        if (num == 0)
                        {
                            DrawSVNOverlays();
                        }
                        else if (num == 1)
                        {
                            if (m_Asset.isFolder)
                            {
                                char[] trimChars = new char[] { '/' };
                                m_Asset = VCDatabase.GetAssetByPath(m_Asset.projectPath.Trim(trimChars) + ".meta");
                                if (m_Asset == null)
                                {
                                    return;
                                }
                            }
                            DrawP4Overlays();
                        }
                    }
                }
            }
        }

        private static void DrawOverlay(string iconName, float xOffset, float yOffset)
        {
            Rect position = new Rect(0f, 0f, 16f, 16f) {
                x = m_ItemRect.x + xOffset,
                y = m_ItemRect.y + yOffset
            };
            GUI.DrawTexture(position, GetOverlayIcon(m_IconPrefix + iconName));
        }

        private static void DrawP4Overlays()
        {
            m_IconPrefix = "P4_";
            float xOffset = 5f;
            float num2 = 4f;
            float yOffset = 1f;
            float num4 = 3f;
            if (IsState(VCAsset.States.AddedLocal))
            {
                DrawOverlay("AddedLocal", -xOffset, -num2);
            }
            if (IsState(VCAsset.States.AddedRemote))
            {
                DrawOverlay("AddedRemote", xOffset, -num2);
            }
            if (IsState(VCAsset.States.CheckedOutLocal) && !IsState(VCAsset.States.LockedLocal))
            {
                DrawOverlay("CheckOutLocal", -xOffset, -xOffset);
            }
            if (IsState(VCAsset.States.CheckedOutRemote) && !IsState(VCAsset.States.LockedRemote))
            {
                DrawOverlay("CheckOutRemote", xOffset, -xOffset);
            }
            if (IsState(VCAsset.States.Conflicted))
            {
                DrawOverlay("Conflicted", -xOffset, yOffset);
            }
            if (IsState(VCAsset.States.Unresolved))
            {
                DrawOverlay("Conflicted", -xOffset, yOffset);
            }
            if (IsState(VCAsset.States.DeletedLocal))
            {
                DrawOverlay("DeletedLocal", -xOffset, -num2);
            }
            if (IsState(VCAsset.States.DeletedRemote))
            {
                DrawOverlay("DeletedRemote", xOffset, -num2);
            }
            if (IsState(VCAsset.States.Local))
            {
                DrawOverlay("Local", -num2, num4);
            }
            if (IsState(VCAsset.States.LockedLocal))
            {
                DrawOverlay("LockedLocal", -xOffset, -num2);
            }
            if (IsState(VCAsset.States.LockedRemote))
            {
                DrawOverlay("LockedRemote", xOffset, -num2);
            }
            if (IsState(VCAsset.States.OutOfSync))
            {
                DrawOverlay("OutOfSync", -xOffset, num4);
            }
        }

        private static void DrawSVNOverlays()
        {
            m_IconPrefix = "SVN_";
            if (IsState(VCAsset.States.AddedLocal))
            {
                DrawOverlay("AddedLocal", -4f, 3f);
            }
            if (IsState(VCAsset.States.Conflicted))
            {
                DrawOverlay("Conflicted", -4f, 2f);
            }
            if (IsState(VCAsset.States.DeletedLocal))
            {
                DrawOverlay("DeletedLocal", -3f, 3f);
            }
            if (IsState(VCAsset.States.Local))
            {
                DrawOverlay("Local", -4f, 1f);
            }
            if (IsState(VCAsset.States.LockedLocal))
            {
                DrawOverlay("LockedLocal", -4f, 2f);
            }
            if (IsState(VCAsset.States.CheckedOutLocal))
            {
                DrawOverlay("OutOfSync", -4f, 3f);
            }
        }

        private static Texture2D GetOverlayIcon(string name)
        {
            if (!overlays.ContainsKey(name))
            {
                overlays[name] = EditorGUIUtility.LoadIcon(name);
            }
            return overlays[name];
        }

        private static bool IsState(VCAsset.States state)
        {
            return m_Asset.IsState(state);
        }
    }
}

